package competition.controller;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import competition.service.UserService;
import okhttp3.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/7/007.
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Resource(name = "userService")
    private UserService userService;


    /**
     * 001.账户注册
     *
     * @param username
     * @param password
     * @param name
     * @return
     */
    @RequestMapping(value = "enroll", method = RequestMethod.POST)
    @ResponseBody
    public Map enroll(@RequestParam String username, @RequestParam String password, @RequestParam String name) {
        Map map;
        map = userService.enroll(username, password, name);
        return map;
    }

    /**
     * 002.账户登录
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Map login(HttpServletRequest request, @RequestParam String username, @RequestParam String password) {
        Map map;
        HttpSession session = request.getSession();

        map = userService.login(username, password);
        if (map.get("status").toString().equals("200")) {

            if (session.getAttribute("username") == null) {
                session.setAttribute("username", username);
                session.setAttribute("user_id", map.get("user_id").toString());

                session.setAttribute("login","false");//指定普通用户登录 管理员session失效


                session.setMaxInactiveInterval(10000);
            }
        }
        map.put("sessionid", request.getRequestedSessionId());
        return map;
    }

    /**
     * 003.设置新密码
     *
     * @param username
     * @param password
     * @param newPassword
     * @return
     */
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    @ResponseBody
    public Map modify(@RequestParam String username, @RequestParam String password, @RequestParam String newPassword) {
        Map map;
        map = userService.login(username, password);
        if (map.get("status").equals("200")) {
            //账号,密码正确,设置新密码
            return userService.modify(username, newPassword);
        } else {
            return map;
        }

    }

    /**
     * 004.查看用户交易总次数
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "consumeTimes", method = RequestMethod.POST)
    @ResponseBody
    public Map consumeTimes(@RequestParam String username) {
        return userService.consumeTimes(username);
    }

    /**
     * 005.返回个人信息
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "personMessage", method = RequestMethod.POST)
    @ResponseBody
    public Map personMessage(@RequestParam String username) {
        return userService.personMessage(username);
    }

    /**
     * 006.查询用户已发布过的交易以及发布交易次数
     *
     * @param
     * @return
     */
    @RequestMapping(value = "queryHadPublishTrade", method = RequestMethod.POST)
    @ResponseBody
    public Map queryHadPublishTrade() {
        return userService.queryHadPublishTrade();
    }

    @RequestMapping(value = "uploadIcon", method = RequestMethod.POST)
    @ResponseBody
    public Map uploadIcon(@RequestParam MultipartFile img, HttpServletRequest request, @RequestParam String username) {

        return userService.uploadIcon(img, request, username);
    }


    @RequestMapping(value = "demoTest", method = RequestMethod.POST)
    @ResponseBody
    public Map demoTest(HttpServletRequest request, @RequestParam String sessionid) {
        HttpSession session = request.getSession();
        Map map = new HashMap();
        Map map1 = new HashMap();
        if (sessionid.equals(request.getRequestedSessionId())) {
            map1.put("user_id", session.getAttribute("user_id"));
            map1.put("message", "登录成功");
            map1.put("status", "200");
            return map1;
        } else {
            map.put("status", "199");
            map.put("message", "未登录，请先登录");
            return map;
        }

    }

    /**
     * 关注话题或取消关注话题（1为关注，0为取消）
     *
     * @param user_id
     * @param top_id
     * @param status
     * @return
     */
    @RequestMapping(value = "focusTop", method = RequestMethod.POST)
    @ResponseBody
    public Map focusTop(@RequestParam int user_id, @RequestParam int top_id, @RequestParam int status) {
        return userService.focusTop(user_id, top_id, status);
    }
//
//    /**
//     * 关注交易或取消关注交易（1为关注，0为取消）
//     *
//     * @param user_id
//     * @param trade_id
//     * @param status
//     * @return
//     */
//    @RequestMapping(value = "focusTrade", method = RequestMethod.POST)
//    @ResponseBody
//    public Map focusTrade(@RequestParam int user_id, @RequestParam int trade_id, @RequestParam int status) {
//        return userService.focusTrade(user_id, trade_id, status);
//    }

    /**
     * 关注或取消关注（status为1为关注，0为取消）
     * @param username
     * @param followName
     * @param status
     * @return
     */
    @RequestMapping(value = "userFollow",method = RequestMethod.POST)
    @ResponseBody
    public Map userFollow(@RequestParam String username, @RequestParam String followName, @RequestParam int status){
        return userService.userFollow(username, followName, status);
    }





    /**
     * 查看粉丝的
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "lookFocus", method = RequestMethod.POST)
    @ResponseBody
    public Map lookFocus(@RequestParam String username,@RequestParam int page, @RequestParam int num) {
        return userService.lookFocus(username,page,num);
    }



    @RequestMapping(value = "lookSelfFocus",method = RequestMethod.POST)
    @ResponseBody
    public Map lookSelfFocus(@RequestParam String  username,@RequestParam int page, @RequestParam int num){

        return userService.lookSelfFocus(username, page, num);
    }

    /**
     * 给指定用户留言
     * @param username
     * @param leaveName
     * @param message
     * @param is_reply  0为留言  1为回复
     * @return
     */
    @RequestMapping(value = "leaveMessageUser",method = RequestMethod.POST)
    @ResponseBody
    public Map leaveMessageUser(@RequestParam String username,@RequestParam String leaveName,@RequestParam String message,@RequestParam int is_reply){
        return userService.leaveMessageUser(username, leaveName, message, is_reply);
    }

    /**
     * 查看别人给自己的留言
     * @param username
     * @return
     */
    @RequestMapping(value = "lookMessageUser",method = RequestMethod.POST)
    @ResponseBody
    public Map lookMessageUser(@RequestParam String username){
        return userService.lookMessageUser(username);
    }

    /**
     * 获取新闻
     * @return
     */

    @RequestMapping(value = "getNews",method = RequestMethod.POST)
    @ResponseBody
    public Map getNews(@RequestParam int page,@RequestParam int num){

        return userService.getNews(page,num);
    }

    @RequestMapping(value = "getTop",method = RequestMethod.POST)
    @ResponseBody
    public Map getTop(@RequestParam String username){

        return userService.getTop(username);
    }











    @RequestMapping(value = "web", method = RequestMethod.POST)
    @ResponseBody
    public Map web() throws IOException {
        Map map = new HashMap();
        String url = "http://baby-api.heiyou.net/index.php/home/index/listBabyAlbum";
        OkHttpClient client = new OkHttpClient();


        FormBody formBody = new FormBody.Builder()
                .add("baby_id", "1")
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            Gson gson = new Gson();
            map = gson.fromJson(response.body().string(), new TypeToken<Map>() {
            }.getType());
            return map;


        } else {
            throw new IOException("Unexpected code " + response);
        }
    }


}
